package com.example;

import java.lang.ref.SoftReference;

/**
 * Created by zhangguobao on 16/11/7.
 */
public class WeekReference {
    public static void main(String[] args) {
        A a = new A();
        a.str = "hello reference";

        SoftReference<A> softReference = new SoftReference<A>(a);
        a = null;
        int i = 0;
        while (softReference.get() != null) {
            i++;
            System.out.println("get str from weekReference "+softReference.get().str+" i == "+i);
            if (i % 10 == 0) {
                System.gc();
                System.out.println("tigger gc");
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(" object was clear");
    }

    static class A {
        public String str;

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }
    }
}
